package com.dubbo.redis;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

/**
 * jedis池使用
 * 
 * @author lipan
 */

public class TleJedisPool {

	private final static Logger log = LoggerFactory.getLogger(JedisDao.class);

	// jedis池
	private static JedisPool writePool;
	// shardedJedis池
	private static ShardedJedisPool readShardPool;

	//
	public static int life;

	public static ResourceBundle bundle;

	/**
	 * 初始化writepool
	 * 
	 * @return JedisPool
	 */
	public static JedisPool getWreitePoolInstance() {
		if (writePool == null) {
			initWritePool();
		}
		return writePool;
	}

	/**
	 * 初始化readpool
	 * 
	 * @return ShardedJedisPool
	 */
	public static ShardedJedisPool getReadPoolInstance() {
		if (readShardPool == null) {
			initReadPool();
		}
		return readShardPool;
	}

	/**
	 * 读取配置文件
	 */
	public static void getConfig() {
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(
                    "/var/lib/db.properties"));
			bundle = new PropertyResourceBundle(in);

			if (bundle == null) {

				throw new IllegalArgumentException(
						"[db.properties] is not found!");

			}

			life = Integer.valueOf(bundle.getString("redis.life")).intValue();

		} catch (Exception e) {
			log.error("getDbSize{}", e.getMessage());
		}

	}

	/**
	 * 静态代码初始化写池配置
	 */
	private static void initWritePool() {
		try {
			if (bundle == null) {
				getConfig();
			}
			/**
			 * 如果你遇到 java.net.SocketTimeoutException: Read timed out
			 * exception的异常信息 请尝试在构造JedisPool的时候设置自己的超时值.
			 * JedisPool默认的超时时间是2秒(单位毫秒)
			 */
			JedisPoolConfig config = new JedisPoolConfig();

			config.setMaxTotal(Integer.valueOf(
					bundle.getString("jedis.pool.maxActive")).intValue());

			config.setMaxIdle(Integer.valueOf(
					bundle.getString("jedis.pool.maxIdle")).intValue());

			config.setMaxWaitMillis(Long.valueOf(
					bundle.getString("jedis.pool.maxWait")).intValue());

			config.setTestOnBorrow(Boolean.valueOf(
					bundle.getString("jedis.pool.testOnBorrow")).booleanValue());

			config.setTestOnReturn(Boolean.valueOf(
					bundle.getString("jedis.pool.testOnReturn")).booleanValue());

			// 根据配置实例化jedis池
			writePool = new JedisPool(config,
					bundle.getString("redis.write.ip.1"), Integer.valueOf(
							bundle.getString("redis.port")).intValue());

		} catch (Exception e) {
			log.error("initWritePool{}", e.getMessage());
		}
	}

	/**
	 * 静态代码初始化读池配置
	 * 
	 */
	private static void initReadPool() {
		try {
			// 加载redis配置文件
			if (bundle == null) {
				getConfig();
			}
			// 创建jedis池配置实例
			JedisPoolConfig config = new JedisPoolConfig();

			// 设置池配置项值

			config.setMaxTotal(Integer.valueOf(
					bundle.getString("jedis.pool.maxActive")).intValue());
			config.setMaxIdle(Integer.valueOf(
					bundle.getString("jedis.pool.maxIdle")).intValue());
			config.setMaxWaitMillis((Long.valueOf(bundle
					.getString("jedis.pool.maxWait")).intValue()));
			config.setTestOnBorrow(Boolean.valueOf(
					bundle.getString("jedis.pool.testOnBorrow")).booleanValue());
			config.setTestOnReturn(Boolean.valueOf(
					bundle.getString("jedis.pool.testOnReturn")).booleanValue());

			// 根据配置实例化jedis池


			// 创建多个redis共享服务

			JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
					bundle.getString("redis.read.ip.1"), Integer.valueOf(
							bundle.getString("redis.port")).intValue());

			JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
					bundle.getString("redis.read.ip.2"), Integer.valueOf(
							bundle.getString("redis.port")).intValue());

			List list = new LinkedList();
			list.add(jedisShardInfo1);
			list.add(jedisShardInfo2);

			// 根据配置文件,创建shared池实例

			readShardPool = new ShardedJedisPool(config, list);

		} catch (Exception e) {
			log.error("initReadPool{}", e.getMessage());
		}
	}

}
